package com.rykj.qxj.rest;


import com.github.pagehelper.PageInfo;
import com.rykj.qxj.annotation.EnableSystemLog;
import com.rykj.qxj.server.model.entity.DatabaseSource;
import com.rykj.qxj.server.services.IDatabaseSourceService;
import com.rykj.qxj.server.util.object.StringUtils;
import com.rykj.qxj.util.RegexUtils;
import com.rykj.qxj.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 数据库源管理
 */
@Log4j
@RestController
@RequestMapping("/v1/databaseSource")
@Api(tags="数据库源管理")
public class DatabaseSourceController {
    @Autowired
    private IDatabaseSourceService sourceService;

    @PostMapping("/saveDatabaseSource")
    @ApiOperation(value="添加数据源库",notes ="添加数据源库")
    @EnableSystemLog(value="添加数据源库",fullPath ="/v1/databaseSource/saveDatabaseSource")
    public Result saveDatabaseSource(@RequestBody DatabaseSource databaseSource){
        try{
            if(RegexUtils.checkSpecialCharacters(databaseSource.getCharCode().replace("-",""))){
                return new Result(false, "字符编码不能包含特殊字符");
            }
            boolean ifExist = sourceService.databaseIfExist(databaseSource.getDatabaseName(),databaseSource.getDatabaseIP());
            if (!ifExist){
                return new Result(false,"数据源信息已存在");
            }
            String password = databaseSource.getPassword().replaceAll("%2B","+");
            databaseSource.setPassword(password);
            String errorMsg= sourceService.saveDatabaseSource(databaseSource);
            if(StringUtils.isNotEmpty(errorMsg)){
                return new Result(false,errorMsg);
            }
            return new Result(true,"添加成功");
        }catch (Exception e){
            log.error("添加失败", e);
            return new Result(false, e.getMessage());
        }
    }
    @GetMapping("/checkConnect")
    @ApiOperation(value="创建连接检测",notes ="创建连接检测")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "databaseIp", value = "连接ip",required = true,paramType = "query"),
            @ApiImplicitParam(name = "port", value = "端口",required = true,paramType = "query"),
            @ApiImplicitParam(name = "userName", value = "用户名",required = true,paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码",required = true,paramType = "query")
    })
    public Result checkConnect(@RequestParam(value = "databaseIp") String databaseIp,
                               @RequestParam(value = "port") String port,
                               @RequestParam(value = "userName") String userName,
                               @RequestParam(value = "password") String password){
        try{
            password = password.replaceAll("%2B","+");
            String errorMsg = sourceService.checkConnect(databaseIp,port,userName,password);
            if (StringUtils.isNotEmpty(errorMsg)){
                return new Result(false,errorMsg);
            }else {
                return new Result(true,"连接成功");
            }
        }catch (Exception e){
            log.error("系统异常", e);
            return new Result(false, "系统异常");
        }
    }


    @PostMapping("/updateDatabaseSource")
    @ApiOperation(value="修改数据源库",notes ="修改数据源库")
    @EnableSystemLog(value="修改数据源库",fullPath ="/v1/databaseSource/updateDatabaseSource")
    public Result updateDatabaseSource(@RequestBody DatabaseSource databaseSource){
        try{
            String password = databaseSource.getPassword().replaceAll("%2B","+");
            databaseSource.setPassword(password);
            sourceService.updateDatabaseSource(databaseSource);
            return new Result(true,"修改成功");
        }catch (Exception e){
            log.error("修改失败", e);
            return new Result(false, "修改失败");
        }
    }
    @GetMapping("/deleteDatabaseSourceById")
    @ApiOperation(value="根据id删除数据源库",notes ="根据id删除数据源库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "baseSourcId", value = "数据库源id",required = true,paramType = "query")
    })
    @EnableSystemLog(value="根据id删除数据源库",fullPath ="/v1/databaseSource/deleteDatabaseSourceById")
    public Result deleteDatabaseSourceById(@RequestParam(value = "baseSourcId") Integer baseSourcId){
        try{
            sourceService.deleteDatabaseSourceById(baseSourcId);
            return new Result(true,"删除成功");
        }catch (Exception e){
            log.error("删除失败", e);
            return new Result(false, "删除失败");
        }
    }

    @GetMapping("/getDatabaseSourceList")
    @ApiOperation(value="分页获取数据源库列表",notes ="分页获取数据源库列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "显示行数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "classification", value = "数据分类",paramType = "query"),
            @ApiImplicitParam(name = "databaseName", value = "数据库名称",paramType = "query"),
            @ApiImplicitParam(name = "databaseCNName", value = "数据库中文名称",paramType = "query"),
            @ApiImplicitParam(name = "directory", value = "数据库目录",paramType = "query"),
            @ApiImplicitParam(name = "appId", value = "应用id主键",paramType = "query"),
            @ApiImplicitParam(name = "databaseType", value = "数据库类型:mysql,redis",paramType = "query")
    })
    public Result getDatabaseSourceList(@RequestParam(value = "pageNum") Integer pageNum,
                                        @RequestParam(value = "pageSize") Integer pageSize,
                                        @RequestParam(value = "classification",required = false) String classification,
                                        @RequestParam(value = "databaseName",required = false) String databaseName,
                                        @RequestParam(value = "databaseCNName",required = false) String databaseCNName,
                                        @RequestParam(value = "directory",required = false) String directory,
                                        @RequestParam(value = "databaseType",required = false) String databaseType,
                                        @RequestParam(value = "appId",required = false) Integer appId){
        try{
           PageInfo pager = sourceService.getDatabaseSourceList(pageNum,pageSize,classification,databaseName,databaseCNName,directory,appId,databaseType);
            return new Result(pager);
        }catch (Exception e){
            log.error("查询失败", e);
            return new Result(false, "查询失败");
        }
    }

    @GetMapping("/getDatabaseSourceDetails")
    @ApiOperation(value="数据源详情",notes ="数据源详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id主键",required = true,paramType = "query")
    })
    public Result getDatabaseSourceDetails( @RequestParam(value = "id") Integer id){
        try{
            DatabaseSource databaseSource   =  sourceService.getDatabaseSourceDetails(id);
            return new Result(databaseSource);
        }catch (Exception e){
            log.error("应用详情失败", e);
            return new Result(false, "应用详情失败");
        }
    }

}
